package com.ykdl.member.kid.fragments;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.adapters.StoreFragmentAdapter;
import com.ykdl.member.kid.beans.ProductBean;
import com.ykdl.member.kid.beans.ProductListBean;
import com.ykdl.member.kid.marketcard.CouponinfoWebView;
import com.ykdl.member.kid.marketcard.NewStoreCoupon;
import com.ykdl.member.kid.marketcard.StoreCouponActivity;
import com.ykdl.member.views.GridViewWithHeaderAndFooter;
import com.ykdl.member.views.PullToRefreshView;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;
/********************************************************************************

** 作者： jiafeiyu
** 创始时间：2015-4-27
** 描述：
**    门店货架的 商店fragment

*********************************************************************************/
public class StoreFragment extends BaseFragment {
	private View convertView;
	private NewStoreCoupon context;
	private int cursor = 0;
	private int count = 20;
	private ArrayList<ProductBean> data_list = new ArrayList<ProductBean>();
	private StoreFragmentAdapter adapter;
	private PullToRefreshGridView pullToResreshView;
	private boolean isAdd = true;
	private String store_id;
	private  GridView gridView;
	@Override
	protected void onNewCreateView(View view) {
		convertView = view;
		setCenterView(R.layout.fragment_store);
		initView();

	}
	public StoreFragment (String store_id) {
		this.store_id=store_id;
	}
	
	private void initView() {
		isAdd = true;
		context = (NewStoreCoupon)getActivity();
		pullToResreshView=(PullToRefreshGridView) convertView.findViewById(R.id.pullToResreshView);
		pullToResreshView.setMode(Mode.BOTH);
		pullToResreshView.setOnRefreshListener(new OnRefreshListener<GridView>() {

			@Override
			public void onRefresh(PullToRefreshBase<GridView> refreshView) {
				// TODO Auto-generated method stub
				
				if(pullToResreshView.isHeaderShown()){
		        	 count = 20; 
		        	 cursor = 0;
		        	 isAdd=true;
		    		 data_list.clear(); 
		    		 getData();
		    		 startLoading();
				}else  if (pullToResreshView.isFooterShown()){
					if (isAdd) {
						count = 20;
						getData();
						startLoading();
					}else {
						pullToResreshView.onRefreshComplete();
					}
				}else {
					pullToResreshView.onRefreshComplete();
				}
			}
		});
		pullToResreshView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				ProductBean bean =(ProductBean)adapter.getItem(arg2);
				Intent intent = new Intent(mContext, CouponinfoWebView.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra(KidAction.PRODUCT_ID, bean.getProduct_id());
				intent.putExtra(KidAction.URL_TYPE, 1);
				intent.putExtra(KidAction.STORE_ID, store_id);
				mContext.startActivity(intent);
			}
		});
		 gridView=pullToResreshView.getRefreshableView();
         gridView.setNumColumns(2);
         adapter = new StoreFragmentAdapter(context);
         gridView.setAdapter(adapter);
         
		setIsShowLoad(true);
		Log.e("alan", "-------------------initView");
		getData();
	}
	public void startLoading() {
		pullToResreshView.getLoadingLayoutProxy(false, true).setPullLabel("上拉加载更多");
		 pullToResreshView.getLoadingLayoutProxy(false, true).setRefreshingLabel("加载中...");
		 pullToResreshView.getLoadingLayoutProxy(false, true).setReleaseLabel("松开后加载");
	}
	/**
	 * activity调用该方法来刷新数据
	 */
	public void getData() {
		// TODO 添加分页获取
		Log.e("alan", "-------------------getData");
		String url = KidConfig.PRODUCT_LIST;
		Map<String, String> map = new HashMap<String, String>();
		map.put("cursor", String.valueOf(cursor));
		map.put("count", String.valueOf(count));
		map.put("store_id", store_id);
		map.put("product_type", "0");
		HttpBaseRequest request = Wxxr.getInstance().getRequest(url, map,
				null);
		TaskManager.startHttpRequest(request, new getDataTag(),
				ProductListBean.class);
	}

	class getDataTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			setIsShowLoad(false);
			if (t instanceof ProductListBean) {
				ProductListBean bean = (ProductListBean) t;
				if (bean.getError() == null) {
					if (null!=bean.getStore()) {
						context.setTitle(bean.getStore().getName());
					}
					if (bean.getData_list() == null
							|| bean.getData_list().size() == 0) {
						gridView.setVisibility(View.GONE);
						showErrorView(true, "尚未录入商品数据");
					} else {
						showErrorView(false, "");
						gridView.setVisibility(View.VISIBLE);
						int size = data_list.size();
						data_list.addAll(bean.getData_list());
						
						adapter.setData(data_list);
						gridView.setAdapter(adapter);
						gridView.setSelection(size);
						if (bean.getNext_cursor() == bean.getTotal_number()) {
							isAdd=false;
						} else {
							cursor = bean.getNext_cursor();
						}
				//		adapter.notifyDataSetChanged();
					}
				}
				pullToResreshView.onRefreshComplete();
			}

		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			pullToResreshView.onRefreshComplete();
			setIsShowLoad(false);
			showErrorView(true, "网络不给力,请检查网络");
			return false;
		}

		@Override
		public void refreshCancel() {
			pullToResreshView.onRefreshComplete();
		}

	}



	@Override
	protected void tryAgain() {
		getData();

	}
}
